using System;
using System.Threading.Tasks;

namespace CKY.AgentPlatform.Domain.McpTools
{
    /// <summary>
    /// MCP客户端管理器接口
    /// </summary>
    public interface IMcpClientManager
    {
        /// <summary>
        /// 执行MCP工具
        /// </summary>
        /// <param name="input">执行输入</param>
        /// <returns>执行结果</returns>
        Task<object> ExecuteToolAsync(McpToolExecutionInput input);

        /// <summary>
        /// 测试MCP连接
        /// </summary>
        /// <param name="serverUrl">服务器URL</param>
        /// <returns>连接是否成功</returns>
        Task<bool> TestConnectionAsync(string serverUrl);

        /// <summary>
        /// 获取MCP服务器信息
        /// </summary>
        /// <param name="serverUrl">服务器URL</param>
        /// <returns>服务器信息</returns>
        Task<McpServerInfo> GetServerInfoAsync(string serverUrl);

        /// <summary>
        /// 获取可用工具列表
        /// </summary>
        /// <param name="serverUrl">服务器URL</param>
        /// <returns>工具列表</returns>
        Task<McpToolList> GetAvailableToolsAsync(string serverUrl);

        /// <summary>
        /// 注册MCP服务器
        /// </summary>
        /// <param name="serverUrl">服务器URL</param>
        /// <param name="serverName">服务器名称</param>
        /// <returns>注册是否成功</returns>
        Task<bool> RegisterServerAsync(string serverUrl, string serverName);

        /// <summary>
        /// 注销MCP服务器
        /// </summary>
        /// <param name="serverUrl">服务器URL</param>
        /// <returns>注销是否成功</returns>
        Task<bool> UnregisterServerAsync(string serverUrl);

        /// <summary>
        /// 获取所有已注册的服务器
        /// </summary>
        /// <returns>服务器列表</returns>
        Task<McpServerInfo[]> GetRegisteredServersAsync();

        /// <summary>
        /// 获取MCP客户端
        /// </summary>
        /// <param name="clientId">客户端ID</param>
        /// <returns>MCP客户端</returns>
        Task<McpClient> GetClientAsync(string clientId);
    }

    /// <summary>
    /// MCP服务器信息
    /// </summary>
    public class McpServerInfo
    {
        /// <summary>
        /// 服务器URL
        /// </summary>
        public string Url { get; set; }

        /// <summary>
        /// 服务器名称
        /// </summary>
        public string Name { get; set; }

        /// <summary>
        /// 服务器版本
        /// </summary>
        public string Version { get; set; }

        /// <summary>
        /// 服务器状态
        /// </summary>
        public McpServerStatus Status { get; set; }

        /// <summary>
        /// 最后连接时间
        /// </summary>
        public System.DateTime? LastConnectedAt { get; set; }

        /// <summary>
        /// 服务器描述
        /// </summary>
        public string Description { get; set; }
    }

    /// <summary>
    /// MCP服务器状态
    /// </summary>
    public enum McpServerStatus
    {
        /// <summary>
        /// 在线
        /// </summary>
        Online = 1,

        /// <summary>
        /// 离线
        /// </summary>
        Offline = 2,

        /// <summary>
        /// 错误
        /// </summary>
        Error = 3
    }

    /// <summary>
    /// MCP工具列表
    /// </summary>
    public class McpToolList
    {
        /// <summary>
        /// 工具列表
        /// </summary>
        public McpToolItem[] Tools { get; set; } = System.Array.Empty<McpToolItem>();

        /// <summary>
        /// 资源列表
        /// </summary>
        public McpResourceItem[] Resources { get; set; } = System.Array.Empty<McpResourceItem>();
    }

    /// <summary>
    /// MCP工具项
    /// </summary>
    public class McpToolItem
    {
        /// <summary>
        /// 工具名称
        /// </summary>
        public string Name { get; set; }

        /// <summary>
        /// 工具描述
        /// </summary>
        public string Description { get; set; }

        /// <summary>
        /// 输入模式
        /// </summary>
        public string InputSchema { get; set; }
    }

    /// <summary>
    /// MCP资源项
    /// </summary>
    public class McpResourceItem
    {
        /// <summary>
        /// 资源URI
        /// </summary>
        public string Uri { get; set; }

        /// <summary>
        /// 资源名称
        /// </summary>
        public string Name { get; set; }

        /// <summary>
        /// 资源描述
        /// </summary>
        public string Description { get; set; }

        /// <summary>
        /// 资源类型
        /// </summary>
        public string MimeType { get; set; }
    }
}